package cn.sdormitory.controller;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.exception.ApiException;
import cn.sdormitory.workflow.entity.HygieneRectificationApply;
import cn.sdormitory.workflow.service.HygieneRectificationApplyService;
import cn.sdormitory.workflow.service.WorkFlowCommonService;
import cn.sdormitory.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: lx
 * @date: 2023/10/16 11:52
 * @description:
 */
@RestController
@RequestMapping("/app/hygieneRectification")
@Slf4j
public class AppHygieneRectificationApplyController {

    @Autowired
    HygieneRectificationApplyService rectificationApplyService;

    @Resource
    WorkFlowCommonService workFlowCommonService;

    /**
     * 提交流程
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/createApply")
    public CommonResult<Integer> createApply(@RequestBody HygieneRectificationApply vo){
        int apply = rectificationApplyService.createApply(vo);
        if(apply>0){
            return CommonResult.success();
        }
        if(apply==-1){
            return CommonResult.failed("学生ID为空");
        }
        if(apply==-2){
            return CommonResult.failed("当天已经提交过处理申请");
        }
        return CommonResult.failed();
    }

    /**
     * 查询申请单
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/pageList")
    public CommonResult<CommonPage<HygieneRectificationApply>> pageList(@RequestBody HygieneRectificationApply vo){
        IPage<HygieneRectificationApply> page = rectificationApplyService.pageList(vo);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 撤销申请单
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/cancelApply")
    public CommonResult<CommonPage<HygieneRectificationApply>> cancelApply(@RequestBody HygieneRectificationApply vo){
        boolean b = rectificationApplyService.cancelApply(vo);
        if(b){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    /**
     * 流程审批
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/processWorkFlow")
    public CommonResult<WorkFlowVO> processWorkFlow(@RequestBody WorkFlowVO vo) throws ApiException{
        workFlowCommonService.processWorkFlow(vo);
        return CommonResult.success();
    }

    /**
     * 获取流程信息
     * @param vo
     * @return
     * @throws ApiException
     */
    @PostMapping("/workFlowInfo")
    public CommonResult<WorkFlowVO> workFlowInfo(@RequestBody WorkFlowVO vo) throws ApiException{
        WorkFlowVO workFlowVO = workFlowCommonService.workFlowInfo(vo);
        if(workFlowVO!=null){
            return CommonResult.success(workFlowVO);
        }
        return CommonResult.failed();
    }
}
